using System;
using System.IO;
using System.Collections;
using System.Reflection;
using Microsoft.RuleEngine;
using Microsoft.BizTalk.RuleEngineExtensions;


namespace DeployRules
{
	partial class App
	{
		/// <summary>
		/// The GetTitle method extracts the assembly title, major version number, and minor 
		/// verson numbers by using the .NET relfection. Then, it returns a string to the caller
		/// that has the sytax "Assembly Title.vMajor Version.Minor Version". AssemblyInfo.cs
		/// file contains the title and version information for the assembly.
		/// </summary>
		/// <returns>Returns "BizTalk 2006 Rule Deployment Tool v1.2" 
		/// where "BizTalk 2006 Rule Deployment Tool" is the assembly title,
		/// "1" is the major version number
		/// and "0" is the minor version number.</returns>
		private static string GetTitle()
		{

			//The varialble to return to the caller; initialized with string.Empty
			string title = string.Empty;

			//Get access to executing Assembly object
			Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();

			//Get the AssemblyTitleAttribute for the assembly in an object array
			object[] attrArray = assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), true);

			//If the title is found
			if (attrArray.Length > 0)
			{
				//Get the title attribute from the object array
				AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attrArray[0];

				//Get the title
				title = titleAttribute.Title;
			}

			//Get the major and minor versions for the assembly
			int majorVersion = assembly.GetName().Version.Major;
			int minorVersion = assembly.GetName().Version.Minor;

			//Append the version numbers to the title by using String.Format method
			title = String.Format("{0} v{1}.{2}", title, majorVersion, minorVersion);

			//Return the title string to the caller
			return title;
		}

		/// <summary>
		/// The LogMessage method writes the input message sting to console, and
		/// writes optionally to a log file.  
		/// </summary>
		/// <param name="message">The string message to be logged</param>  
		private static void LogMessage(string message)
		{
			// Log to console
			Console.WriteLine(message);

			// Log to file
			if (App.logWriter != null)
				App.logWriter.WriteLine(message);
		}

		/// <summary>
		/// The ExtractPolicyNameMajorMinor takes a string that is in policyName.majorVer.minorVer format, 
		/// and sets the values for the policyName, policyMajorVer and policyMinorVer fields. 
		/// </summary>
		/// <param name="args">string containing policy name, optionally major and minor version</param>
		/// <returns></returns>
		private static void ExtractPolicyNameMajorMinor(string polNameWithVersion)
		{
			string[] arr = polNameWithVersion.Split('.');

			if (arr.Length == 0) //If no policy name is specified, export all policies
			{
				exportAllPolicies = true;
			}

			if (arr.Length >= 1) //If only policy name is specified, no version
			{
				App.policyName = arr[0];
			}

			if (arr.Length >= 2) //If policy name and major versions are specified
			{
				App.policyMajorVer = System.Int32.Parse(arr[1]);
			}
			if (arr.Length >= 3) //if policy name, major and minor versions are specified
			{
				App.policyMinorVer = System.Int32.Parse(arr[2]);
			}
		}

		/// <summary>
		/// The ExtractVocabularyNameMajorMinor takes a string in vocabularyName.majorVer.minorVer format, 
		/// and sets the values for the vocabularyName, policyMajorVer and policyMinorVer fields. 
		/// </summary>
		/// <param name="args">string containing vocabulary name, optionally major and minor version</param>
		/// <returns></returns>
		private static void ExtractVocabularyNameMajorMinor(string vocabularyNameWithVer)
		{
			string[] arr = vocabularyNameWithVer.Split('.');


			if (arr.Length == 0)
			{
				exportAllVocabularies = true;
			}

			if (arr.Length >= 1)
			{
				App.vocabularyName = arr[0];
			}

			if (arr.Length >= 2)
			{
				App.vocabularyMajorVer = System.Int32.Parse(arr[1]);
			}
			if (arr.Length >= 3)
			{
				App.vocabularyMinorVer = System.Int32.Parse(arr[2]);
			}

		}
	}
}
